<?php
/*
    [Yunflv.com] (C)2016 云解析 请根据注释填写相应内容。
	请在此地址输入用户token查询统计情况。
	http://42.96.194.52:16131/cnzz/sourcecommon/user.php
*/

/*------------用户配置区域--------------*/
$whitelist = array("http://m.1haza.com", "http://v.1haza.com", "http://127.0.0.1",); 
//AAA.自己有几个站用就写几个,需要www就写www，不需要带www就不写，都有就都写，使用英文逗号分隔，不支持通配符*,带http://，最后一个地址在);之前不加,
define('NAME', '531660000'); //CCC.填写购买时候的QQ号码，纯数字
define('TOKEN', '3d4852ad2bf1293a3551a1beef0869e3'); //DDD.请填写用户token验证码，字符串
define('USER', 'https://s.19av.net/vod/api/url.php'); //EEE.填写当前文件url.php存放的路径，是绝对地址，带http://
define('HD', '3'); //FFF.默认分辨率,3-高清 2-标清  1-低清
define('SWF', '0'); //GGG.如果只用url和id解析,在此处填写0，需要支持SWF的链接形式解析，在此处填写1
/*------------用户配置结束---------------*/
/*------------系统配置区域,请勿修改--------------*/
define('API', 'http://139.129.25.160:12135/api/i.php'); //千万不要修改这里
define('SOURCE', 'http://42.96.194.52:16131/cnzz/sourcecommon/api.php'); //大爷别闹,这里是统计api,不能修改
error_reporting(E_ERROR | E_WARNING | E_PARSE);
error_reporting(0);
$error = "http://www.yunflv.com/";
$reffer = $_SERVER['HTTP_REFERER'];
if (!isset($_GET['debug']) || $_GET['ctype'] == 'phone') {
    if ($reffer) {
        while (list($d, $s) = each($whitelist)) {
            if (ereg($s, "$reffer")) {
                $y = 1;
            }
        }
    }
    if ($_GET['ctype'] == 'phone') {
        $y = 1;
    }
    if ($y != 1) {
        header("Location:" . $error);
        return;
    }
}
ob_start();
$url = $_GET['url'];
$id = $_GET['id'];
$type = $_GET['type'];
$hd = $_GET['hd'] ? $_GET['hd'] : HD;
$ctype = $_GET['ctype'] ? $_GET['ctype'] : 'pc';
$change = array('yk' => 'youku', 'td' => 'tudou');
foreach ($change as $k => $v) {
    if ($type == $k) {
        $type = $v;
    }
}
$url = check_in(array('qq.com', 'joy.cn', 'lovev.com', 'tucao', 'maiquan', 'weibo', 'dwstatic.com', 'plures.net'), urldecode($url));
if (SWF == '1') {
    if (strpos($url, 'static.video.qq.com')) {
        preg_match('/vid=(.*)\//U', $url . '/', $a);
        $url = $a[1] . '_qq';
    }
    if (strstr($url, 'vrs.sohu.com')) {
        preg_match('/com\/(.*)\/v.swf/U', $url . '/', $a);
        $url = $a[1] . '_sohu';
    }
    if (strstr($url, 'player.youku.com')) {
        preg_match('/sid\/(.*)\/v.swf/U', $url . '/', $a);
        $url = $a[1] . '_youku';
    }
    if (strstr($url, 'i7.imgs.letv.com')) {
        preg_match('/id=(.*)\//U', $url . '/', $a);
        $url = $a[1] . '_letv';
    }
    if (strstr($url, 'tudou.com') && strstr($url, '.swf')) {
        $turl = get_jump_url($url);
        $iid = inter('iid=', '&', $turl);
        $url = $iid . '_tudou';
    }
    if (strstr($url, 'weibo.com') && strstr($url, '.swf')) {
        preg_match('/1034:(.*)\//U', $url, $a);
        $url = 'http://video.weibo.com/show/fid=1034:' . $a[1];
    }
	
	if (strstr($url, 'assets.dwstatic.com')) {
        preg_match('/vid=(.*)\//U', $url . '/', $a);
        $url = $a[1] . '_huya';
    }

    if (strstr($url, 'r.plures.net')) {
        preg_match('/cloudMediaId=(.*)\//U', $url . '/', $a);
        $url = $a[1] . '_longzhu';
    }	
    
	if (strstr($url, 'player.cntv.cn')) {
        preg_match('/pid=(\w+)/ise', $url, $a);
        $url = $a[1] . '_cntv';
    }

    if (strstr($url, 'player.ku6.com')) {
        preg_match('/ku6.com\/.*\/(.*)\/v.swf/U', $url . '/', $a);
        $url = $a[1] . '_ku6';
    }
}
$param = $url ? 'url=' . $url : 'id=' . $id . '&type=' . $type;
$source = $url ? $url : $id . '_' . $type;
@curl(SOURCE . '?user=yunflv' . NAME . '&from=' . $source . '&ly=' . $reffer);
//@curl(SOURCE . '?user=yunflv' . NAME . '&from=' . $url . '&ly=' . $reffer);
switch ($ctype) {
    case 'phone':
        $api = API . '?token=' . TOKEN . '&' . $param . '&hd=' . $hd . '&ctype=m';
        $con = curl($api);
        $json = json_decode($con, 1);
        $url = $json['m'];
        header('Content-Type:application/force-download');
        header("Location:" . $url);
        break;

    default:
        header('Content-type:text/xml;charset=utf-8');
        $api = curl(API . '?token=' . TOKEN . '&' . $param . '&hd=' . $hd);
        $api = str_replace('f->?url=', 'f->' . USER . '?url=', $api);
        echo $api;
        break;
}
function curl($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    $src = curl_exec($curl);
    curl_close($curl);
    return $src;
}
function check_in($arr, $url) {
    foreach ($arr as $key) {
        if (strstr($url, $key) != '') {
            $url = str_replace(array('?', '&', '#'), array('/', '/', ''), urldecode($url));
        }
    }
    return $url;
}
function get_jump_url($url) {
    $url = str_replace(' ', '', $url);
    do {
        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 10);
        $header = curl_exec($curl);
        curl_close($curl);
        preg_match('|Location:\s(.*?)\s|', $header, $tdl);
        if (strpos($header, "Location:")) {
            $url = $tdl ? $tdl[1] : null;
        } 
        else {
            return $url . '';
            break;
        }
    } while (strpos($header, "Location:"));
}
function inter($start, $end, $str) {
    $wd2 = '';
    if ($str && $start) {
        $arr = explode($start, $str);
        if (count($arr) > 1) {
            $wd = $arr[1];
            if ($end) {
                $arr2 = explode($end, $wd);
                if (count($arr2) > 1) {
                    $wd2 = $arr2[0];
                } 
                else {
                    $wd2 = $wd;
                }
            } 
            else {
                $wd2 = $wd;
            }
        }
    }
    return $wd2;
}
?>